package com.mindao.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom2.Content;
import org.jdom2.Content.CType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(tags = "DevOps平台对外开放接口")
@RequestMapping("/tools")
public class toolsController {

   
    @GetMapping("/list")
    @ApiOperation("查询所有可用工具")
    @CrossOrigin
    public ResponseEntity<HashMap> list() throws Exception {
		HashMap toolsMap = new HashMap();
		File file = new File("pom.xml");
		SAXBuilder sb = new SAXBuilder();
		Document doc = sb.build(file);
		// 构造文档对象
		Element root = doc.getRootElement(); // 获取根元素HD
		for (Iterator iterator = root.getChildren().iterator(); iterator.hasNext();) {
			Element child = (Element) iterator.next(); 
			if (child.getName().equals("dependencies")) { //直接根据名字获取，无法获取子
				for (Iterator iterator2 = child.getChildren().iterator(); iterator2.hasNext();) {
					Element depend = (Element) iterator2.next();
					//System.out.println("依赖" + depend.getName());
				}
				List<Content> contents = child.getContent();
				String url = "";
				String toolName = "";
				for (Content con : contents) {// CType为一个枚举类，一共有七种，其中Comment代表注释
				   // System.out.println(con.getCType()+"：\n" + con.getValue());
					if (con.getCType().equals(CType.Comment)) {
						//System.out.println("注释：" + con.getValue());
						url = con.getValue().trim();

					}
					if (con.getCType().equals(CType.Element)) {
						// System.out.println("元素："+con.getValue());
						String[] jar = con.getValue().split("\n");
						//System.out.println(jar[2]);
						toolName = jar[2].trim();
						if (toolName.contains("tool-")) {
							toolsMap.put(toolName, url);
						}
					}
				}
			}
		}
       
    	return new ResponseEntity<HashMap>(toolsMap, HttpStatus.OK);
    }
    
    
}